<template>
  <a-modal
    title="操作"
    style="top: 20px;"
    :width="800"
    v-model="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleSubmit"
  >
    <a-form :form="form">

      <a-form-item
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
        label="索引类型">
        <a-select
          v-decorator="['type',{rules: [{ required: true, message: '请选择索引类型' }], initialValue: 'all'}]"
        >
          <a-select-option v-for="type in typeOptions" :key="type.key" :value="type.key">{{
            type.value
          }}</a-select-option>
        </a-select>
      </a-form-item>

    </a-form>
  </a-modal>
</template>
<script>
import { indicesTypes, reIndex } from '@/api/system'
export default {
  name: 'ReIndexModal',
  components: {
  },
  data () {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      typeOptions: [],
      confirmLoading: false,
      form: this.$form.createForm(this)
    }
  },
  beforeCreate () {
  },
  created () {
  },
  methods: {
    reIndex () {
      this.form.resetFields()
      indicesTypes({}).then(res => {
        this.typeOptions = []
        if (res.status === 1) {
          this.typeOptions.push({ key: 'all', value: '全部' })
          if (res.data.length) {
            this.typeOptions = this.typeOptions.concat(res.data)
          }
        }
      })
      this.$nextTick(() => {
        this.form.setFieldsValue({ 'type': 'all' })
      })
      this.visible = true
    },
    handleSubmit (e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          this.confirmLoading = true
          reIndex(values).then(res => {
            if (res.status === 1) {
              this.$message.success(res.message)
              this.visible = false
            } else {
              this.$message.error(res.message)
            }
          }).finally(() => {
            this.confirmLoading = false
          })
        }
      })
    }
  },
  watch: {
  }
}
</script>
